<?php
/**
 * 查询控制器
 */
namespace app\manage\controller\admin;
use app\manage\controller\Base;

class Api extends Base
{
	/**
	 * 管理员查询
	 * @return [type] [description]
	 */
	public function admin_rows(){
		$params = $this->request->param();
        
        $perpagenum = isset($params['limit'])?intval($params['limit']):15;
        $perpagenum = $perpagenum>100?100:$perpagenum;
        $page = isset($params['page'])?intval($params['page']):1;
        $page = $page<1?1:$page;

        unset($params['limit']);
        unset($params['page']);
        $where = [];
        if(isset($params['key'])){
            $where= parseWhere($params['key']);
        }

        $data = model('admin')->getPageDatas($where,$page,$perpagenum);
        $data['code'] = 1;
        return json($data);
	}
	
	/**
	 * 查询单条记录
	 * @return [type] [description]
	 */
	public function admin_row(){
		$params = $this->request->param();
		$id = $params['id'];

		$data = model('admin')->find($id);
		if($data){
			return json(['code'=>1,'data'=>$data]);
		}else{
			return json(['code'=>0,'msg'=>'用户不存在']);
		}
	}
	/**
	 * 禁用状态接口
	 * @return [type] [description]
	 */
	public function admin_disable(){
		$params = $this->request->param();
		$disable = isset($params['disable'])?$params['disable']:null;
		$data = model('admin')::disable($disable);
		if($data){
			return json(['code'=>1,'data'=>$data]);
		}else{
			return json(['code'=>0,'data'=>'']);
		}
	}

	/**
	 * 用户组权限接口
	 * @return [type] [description]
	 */
	public function auth_rows(){
		$params = $this->request->param();
		$id = isset($params['id'])?$params['id']:null;

		$result = \app\common\model\AdminAuth::adminAuthData($id);

		$data = [];
		if(is_null($id)){
			foreach($result as $value):
				preg_match('/manage\/([a-z]+?)\./', $value['auth_path'],$match);
				if(isset($match[1])){
					$data[$match[1]][]=$value;
				}else{
					$data['other'][]=$value;
				}
			endforeach;
		}else{
			$data = $result;
		}

		return json(['code'=>1,'data'=>$data]);
	}

	/**
	 * 用户组列表
	 * @return [type] [description]
	 */
	public function group_rows(){
		$params = $this->request->param();

		$id = isset($params['id'])?$params['id']:null;

		$data = \app\common\model\AdminGroup::adminGroupData($id);
		if($data){
			if(!is_null($id)){
				$data['group_auth'] = unserialize($data['group_auth']);
			}
			return json(['code'=>1,'data'=>$data]);
		}else{
			return json(['code'=>0]);
		}
	}

	

	

}